/*
 * Project: Sudoku Hint Engine for Android
 * Copyright (C) 2010-2013 Emanuel Moecklin
 * 
 * Available under the terms of the Lesser General Public License (LGPL)
 */
package com.onegravity.sudoku.solver.rules.als;

import java.util.BitSet;

public class CommonCandidates {
	
	private final ALS als1;
	private final ALS als2;
	
	private final int nrOfCommonCandidates;
	private final NumberSet commonCandidates = new NumberSet();
	
	private int nrOfRestrictedCommonCandidates;
	private final NumberSet restrictedCommonCandidates = new NumberSet();
	
	CommonCandidates(ALS als1, ALS als2) {
		this.als1 = als1;
		this.als2 = als2;
		BitSet candidates1 = als1.getPotentials();
		BitSet candidates2 = als2.getPotentials();
		int counter = 0;
		for (int value=1; value<=9; value++) {
			boolean isCommon = (candidates1.get(value) && candidates2.get(value));
			if (isCommon) {
				commonCandidates.add(value);
				counter++;
			}
		}
		nrOfCommonCandidates = counter;
		nrOfRestrictedCommonCandidates = 0;
	}

	ALS getALS1() {
		return als1;
	}
	
	ALS getALS2() {
		return als2;
	}
	
	ALS getOtherALS(ALS als) {
		return als.equals(als1) ? als2 : als.equals(als2) ? als1: null;
	}
	
	int getNrOfCommonCandidates() {
		return nrOfCommonCandidates;
	}

	NumberSet getCommonCandidates() {
		return commonCandidates;
	}

	boolean hasRCCs() {
		return (nrOfRestrictedCommonCandidates>0);
	}

	int getNrOfRestrictedCommonCandidates() {
		return nrOfRestrictedCommonCandidates;
	}

	NumberSet getRestrictedCommonCandidates() {
		return restrictedCommonCandidates;
	}

	void addRestrictedCommonCandidate(int value) {
		if (!restrictedCommonCandidates.contains(value)) {
			restrictedCommonCandidates.add(value);
			nrOfRestrictedCommonCandidates++;
		}
	}
	
	@Override
	public String toString() {
		StringBuffer result = new StringBuffer();
		result.append("als1:\n" + als1 + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
		result.append("als2:\n" + als2 + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
		result.append("commonCandidates:          \n" + commonCandidates + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
		result.append("restrictedCommonCandidates:\n" + restrictedCommonCandidates + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
		return result.toString();
	}
}